//
//  UIView+Util.h
//  wozhua
//
//  uiview 基础扩展
//
//  Created by 王洋 on 14-10-13.
//  Copyright (c) 2014年 王洋. All rights reserved.
//
//  copy from https://github.com/nicklockwood/ViewUtils
//

#import <UIKit/UIKit.h>
#import "Masonry.h"

//uiview border的位置
typedef enum {
    UIViewBorderPositionTop,
    UIViewBorderPositionRight,
    UIViewBorderPositionBottom,
    UIViewBorderPositionLeft,
} UIViewBorderPosition;

typedef enum {
    UIViewPositionTopLeft,
    UIViewPositionTopRight,
    UIViewPositionBottomLeft,
    UIViewPositionBottomRight,
    UIViewPositionCenter,
} UIViewPosition;

@interface UIView (Util)

//nib loading

+ (id)instanceWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)bundleOrNil owner:(id)owner;
- (void)loadContentsWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)bundleOrNil;

//hierarchy

- (UIView *)viewMatchingPredicate:(NSPredicate *)predicate;
- (UIView *)viewWithTag:(NSInteger)tag ofClass:(Class)viewClass;
- (UIView *)viewOfClass:(Class)viewClass;
- (NSArray *)viewsMatchingPredicate:(NSPredicate *)predicate;
- (NSArray *)viewsWithTag:(NSInteger)tag;
- (NSArray *)viewsWithTag:(NSInteger)tag ofClass:(Class)viewClass;
- (NSArray *)viewsOfClass:(Class)viewClass;

- (UIView *)firstSuperviewMatchingPredicate:(NSPredicate *)predicate;
- (UIView *)firstSuperviewOfClass:(Class)viewClass;
- (UIView *)firstSuperviewWithTag:(NSInteger)tag;
- (UIView *)firstSuperviewWithTag:(NSInteger)tag ofClass:(Class)viewClass;

- (BOOL)viewOrAnySuperviewMatchesPredicate:(NSPredicate *)predicate;
- (BOOL)viewOrAnySuperviewIsKindOfClass:(Class)viewClass;
- (BOOL)isSuperviewOfView:(UIView *)view;
- (BOOL)isSubviewOfView:(UIView *)view;

- (UIViewController *)firstViewController;
- (UIView *)firstResponder;

//frame accessors

@property (nonatomic, assign) CGPoint origin;
@property (nonatomic, assign) CGSize size;
@property (nonatomic, assign) CGFloat top;
@property (nonatomic, assign) CGFloat left;
@property (nonatomic, assign) CGFloat bottom;
@property (nonatomic, assign) CGFloat right;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;

//bounds accessors

@property (nonatomic, assign) CGSize boundsSize;
@property (nonatomic, assign) CGFloat boundsWidth;
@property (nonatomic, assign) CGFloat boundsHeight;

//content getters

@property (nonatomic, readonly) CGRect contentBounds;
@property (nonatomic, readonly) CGPoint contentCenter;

//扩展常用属性
@property (nonatomic, assign) BOOL isInterfaceBuilder;
@property (nonatomic, strong) IBInspectable NSString *borderColorStr;
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
@property (nonatomic, assign) IBInspectable CGFloat borderWidth;
@property (nonatomic, strong) IBInspectable UIColor *borderColor;
@property (nonatomic, strong) IBInspectable NSString *bgColorStr;

//additional frame setters

- (void)setLeft:(CGFloat)left right:(CGFloat)right;
- (void)setWidth:(CGFloat)width right:(CGFloat)right;
- (void)setTop:(CGFloat)top bottom:(CGFloat)bottom;
- (void)setHeight:(CGFloat)height bottom:(CGFloat)bottom;

//animation

- (void)crossfadeWithDuration:(NSTimeInterval)duration;
- (void)crossfadeWithDuration:(NSTimeInterval)duration completion:(void (^)(void))completion;

//constraints

- (void)customAddConstraints:(NSArray *)constraints;
- (void)removeAllConstraints;
- (void)autoresizesSubview:(UIView *)subview withConstraint:(UIEdgeInsets)padding;
- (void)positionSubview:(UIView *)subview withConstraint:(CGPoint)point;
- (void)positionSubview:(UIView *)subview atPosition:(UIViewPosition)position constraint:(CGPoint)point;
- (void)addSizeConstraint:(CGSize)size;

//tap
- (UITapGestureRecognizer *)addTapGesture:(void (^)(UITapGestureRecognizer *tapGestureRecognizer))aBlock;
- (void)addTapGestureWithBlock:(void(^)(void))aBlock;
- (void)removeTapGesture;

//border

- (UIView *)customBorderWithSize:(CGFloat)size andPosition:(UIViewBorderPosition)position andColor:(UIColor *)color andLeadingInset:(CGFloat)leadingInset andTrailingInset:(CGFloat)trailingInset;

@end
